package adapter;

import java.util.List;

import com.example.lastprojectbeautiplus.R;

import utils.ProductSize;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

public class SizeAndQuantityAdapter extends BaseAdapter {
	private Activity activity;
	private List<ProductSize> productSize;
	private View view;

	public SizeAndQuantityAdapter(Activity activity,
			List<ProductSize> productSize) {
		this.activity = activity;
		this.productSize = productSize;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return productSize.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return productSize.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) activity
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.size_and_quantity_add_product,
					null);

		} else
			view = (View) convertView;
		DisplayMetrics metrics = view.getResources().getDisplayMetrics();
		int width = metrics.widthPixels;
		final EditText etSize = (EditText) view.findViewById(R.id.etSize);
		final EditText etQuantity = (EditText) view
				.findViewById(R.id.etQuantity);
		etSize.setText(productSize.get(position).getSize());
		etQuantity.setText(productSize.get(position).getQuantity() + "");
		etSize.getLayoutParams().width = width / 2;

		etSize.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (!hasFocus) {
					productSize.get(position).setSize(
							etSize.getText().toString());
				}
			}
		});
		etQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (!hasFocus) {
					productSize.get(position).setQuantity(
							Integer.parseInt(etQuantity.getText().toString()));
				}
			}
		});
		return view;
	}

}
